package elems;

import common.ParseException;
import common.Strings;

import java.util.ArrayList;

public class Selector {
    private String selector;
    private String comment;

    private Selector(String selector, String comment) {
        this.selector = selector;
        this.comment = comment;
    }

    public String getSelector() {
        return selector;
    }

    public String getCss() {
        return selector + " /* [SELECTOR] " + comment + " */";
    }

    public static boolean is(int lineNum, String text) throws ParseException {
        String[] psevdoClasses = {
                ":-moz-placeholder",
                ":active",
                ":after",
                ":before",
                ":checked",
                ":default",
                ":disabled",
                ":empty",
                ":enabled",
                ":first-child",
                ":first-letter",
                ":first-line",
                ":first-of-type",
                ":focus",
                ":hover",
                ":indeterminate",
                ":invalid",
                ":lang",
                ":last-child",
                ":last-of-type",
                ":link",
                ":not",
                ":nth-child",
                ":nth-last-child",
                ":nth-last-of-type",
                ":nth-of-type",
                ":only-child",
                ":only-of-type",
                ":optional",
                ":read-only",
                ":read-write",
                ":required",
                ":root",
                ":target",
                ":valid",
                ":visited",
                "::-moz-selection",
                "::after",
                "::before",
                "::first-letter",
                "::first-line",
                "::selection"
        }; // String[] psevdoClasses

        if (text.trim().startsWith("~selector{")) {
            if (text.trim().endsWith("}")) {
                return true;
            } else
                throw new ParseException("Строка селектока начинающаяся на '~selector{' не заканчивается '}' (Строка " + lineNum + ")");
        }

        ArrayList<String> ttt = Strings.split(lineNum, Strings.trimAndDelDoubleSpace(text), "//", true, true);
        String tText = ttt.get(0).trim();

        if (tText.length() == 0)
            return false;

        if (tText.startsWith("@"))
            return false;

        int i = tText.indexOf(':');

        if (i < 0)
            return true;
        tText = tText.toLowerCase();
        for (String str : psevdoClasses)
            if (i == tText.indexOf(str))
                return true;
        return false;
    }

    public static ArrayList<Selector> get(Clazz parent, int lineNum, String text) throws ParseException {
        if (!is(lineNum, text))
            return null;

        ArrayList<Selector> selectors = new ArrayList<Selector>();
        if (text.trim().startsWith("~selector{")) {
            selectors.add(new Selector(text.trim().substring(3,text.trim().length()-1), "STATIC"));
        } else {

            ArrayList<String> ttt = Strings.split(lineNum, Strings.trimAndDelDoubleSpace(text), "//", true, true);
            String tText = ttt.get(0);
            String tComment = (ttt.size() > 1) ? ttt.get(1).trim() : "";

            // ToDO: реализовать подстановук селекторов по анкору. Н.р.
            // body table
            // @@anc1()
            //     какое-то там тело
            // @@anc1 tr td
            //     какое-то там тело
            selectors.add(new Selector(tText, tComment));
        }
        return selectors;
    }
}
